package com.free.others;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

/**
 * <p>
 * Title: Eclipse Plugin Development
 * </p>
 * <p>
 * Description: Free download
 * </p>
 * <p>
 * mail: ganshm@gmail.com
 * </p>
 */

public class GcExample
{
    
    public static void main(String[] args)
    {
        Display display = new Display();
        Shell shell = new Shell(display);
        shell.setText("Drawing Example");
        
        final Canvas canvas = new Canvas(shell, SWT.NONE);
        canvas.setSize(150, 150);
        canvas.setLocation(20, 20);
        shell.open();
        shell.setSize(200, 220);
        
//    canvas.addPaintListener(new PaintListener() {
//	    public void paintControl(PaintEvent e) {
//	    	GC gc = (GC)e.gc;
//	        gc.drawRectangle(10, 10, 40, 45);
//	        gc.drawOval(65, 10, 30, 35);
//	        gc.drawLine(130, 10, 90, 80);
//	        gc.drawPolygon(new int[] { 20, 70, 45, 90, 70, 70 });
//	        gc.drawPolyline(new int[] { 10, 120, 70, 100, 100, 130, 130, 75 });
//	    }
//	  });
        
        // 新建GC对象
        GC gc = new GC(canvas);
        // 调用画图方法画矩形
        gc.drawRectangle(10, 10, 40, 45);
        gc.drawOval(65, 10, 30, 35); // 椭圆
        gc.drawLine(130, 10, 90, 80);
        gc.drawPolygon(new int[] {20, 70, 45, 90, 70, 70});
        gc.drawPolyline(new int[] {10, 120, 70, 100, 100, 130, 130, 75});
        // 释放GC资源
        gc.dispose();
        
        while (!shell.isDisposed())
        {
            if (!display.readAndDispatch())
                display.sleep();
        }
        display.dispose();
    }
}
